﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;


namespace ReplaceCsv
{
    class ConvertCsv : InputOutputConverter
    {
        string _inputFileName;
        protected string inputFileName
        {
            set { this._inputFileName = value; }
            get { return this._inputFileName; }
        }
        string _outputFileName;
        protected string outputFileName
        {
            set { this._outputFileName = value; }
            get { return this._outputFileName; }
        }
        public ConvertCsv() { }
        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="inputFileName"></param>
        /// <param name="outputFileName"></param>
        protected ConvertCsv(string input, string output)
        {
            this.inputFileName = input;
            this.outputFileName = output;
        }
        /// <summary>
        /// インスタンスを生成して返却する
        /// </summary>
        /// <param name="inputFileName">入力ファイル名（あらかじめ検査済であること）</param>
        /// <param name="outputFilename">出力ファイル名（あらかじめ検査済であること）</param>
        /// <returns></returns>
        public virtual InputOutputConverter createInstance(string inputFileName, string outputFileName)
        {
            ConvertCsv result = new ConvertCsv(inputFileName, outputFileName);
            return result;
        }
        /// <summary>
        /// 変更処理を実行する。
        /// </summary>
        /// <returns></returns>
        public virtual  bool execute()
        {
            List<regConv> convertmap = makeConvertMapPilot();

            return fileConvert(convertmap);
        }
        public virtual bool execute(ProgressBar bar)
        {
            return this.execute();
        }

        /// <summary>
        /// 入力ファイルの拡張子を返却する
        /// </summary>
        /// <returns></returns>
        public string getInputExt()
        {
            return "csv";
        }
        /// <summary>
        /// 出力ファイルの拡張子を返却する
        /// </summary>
        /// <returns></returns>
        public string getOutputExt()
        {
            return "csv";
        }

        private bool fileConvert(List<regConv> convmap)
        {
            bool result = false;
            using (FileStream f = File.OpenRead(this.inputFileName))
            {
                using (FileStream u = File.OpenWrite(this.outputFileName))
                {
                    //CP932
                    TextReader inCsv = new StreamReader(f, Encoding.GetEncoding(932));
                    TextWriter outCsv = new StreamWriter(u, Encoding.GetEncoding(932));
                    String line;
                    while ((line = inCsv.ReadLine()) != null)
                    {
                        outCsv.WriteLine(this.convert(convmap, line));
                    }
                    outCsv.Flush();
                    result = true;
                }
            }

            return result;
        }
        private string convert(List<regConv> convmap,string line) 
        {
            string result = line ;
            foreach(regConv cnv in convmap){
                result = cnv.key.Replace(result,cnv.replaceString);
            }
            return result;
        }
        /// <summary>
        /// 置換テスト用のマップを生成
        /// </summary>
        /// <returns></returns>
        private List<regConv> makeConvertMapPilot()
        {
            List<regConv> result = new List<regConv>();
            using (FileStream f = File.OpenRead(@".\\_ReplaceMap.tcsv"))
            {
                //CP932
                TextReader txt = new StreamReader(f, Encoding.GetEncoding(932));
                String line ;
                while((line = txt.ReadLine()) != null){
                    string[] map = line.Split('\t');
                    if(map[0] != null && !map[0].Equals("")){
                        result.Add(new regConv(map[0], map[1]));
                    }
                }
            }
            return result;
        }

        class regConv{
            public Regex key;
            public string replaceString;
            public regConv(string key, string value)
            {
                //正規表現のメタ文字をどう置換するかは別途検討する 20120619
                this.key =  new Regex(key);
                this.replaceString = value;
            }
        }
    }
}
